package com.hht.tools.uilib

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import com.hht.toolanduilibs.showToast
import com.hht.toolanduilibs.widget.BaseClockView
import com.hht.tools.R
import com.hht.tools.databinding.HomeClockViewBinding
import java.util.*


/**
 * Author:chen'hu
 * Time:2023/8/4 15:52
 * Description:This is HomeClockView
 */
class HomeClockView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null,
) : BaseClockView(context, attrs) {

    private lateinit var binding: HomeClockViewBinding
    private var isYearMonthDayFormat = false

    //星期字串数组
    private val weekResIds = arrayOf(0,
        R.string.data_sunday_short,
        R.string.data_monday_short,
        R.string.data_tuesday_short,
        R.string.data_wednesday_short,
        R.string.data_thursday_short,
        R.string.data_friday_short,
        R.string.data_saturday_short)

    private val monthIds = arrayOf(
        "0",
        R.string.data_january_short,
        R.string.data_february_short,
        R.string.data_march_short,
        R.string.data_april_short,
        R.string.data_may_short,
        R.string.data_june_short,
        R.string.data_july_short,
        R.string.data_august_short,
        R.string.data_september_short,
        R.string.data_october_short,
        R.string.data_november_short,
        R.string.data_december_short
    )

    init {
        initView()
    }

    private fun initView() {
        binding = HomeClockViewBinding.inflate(LayoutInflater.from(context), this, true)
        binding.homeTvData.paint.isAntiAlias = true
        binding.homeTvTime.paint.isAntiAlias = true
        binding.homeTvAmPm.paint.isAntiAlias = true
        updateCurrentClock()
        binding.homeTvData.setOnClickListener {
           "homeTvData".showToast(context)
        }
    }

    fun setYearMonthDayFormat(isYear: Boolean) {
        isYearMonthDayFormat = isYear
    }

    fun setTextViewColor(color: Int) {
        binding.homeTvTime.setTextColor(color)
        binding.homeTvData.setTextColor(color)
        binding.homeTvAmPm.setTextColor(color)
    }

    @SuppressLint("SetTextI18n")
    override fun updateTime(
        currentTime: String?,
        currentYear: String?,
        currentMonth: String?,
        currentDay: String?,
        weekNumber: Int,
    ) {
        binding.homeTvTime.text = currentTime?.substring(0, 5)
        val weekStringId = weekResIds[weekNumber]
        val monthStringId: Int = monthIds[currentMonth!!.toInt()] as Int
        if (isYearMonthDayFormat) {
            binding.homeTvData.text =
                "$currentYear-$currentMonth-$currentDay  ${context.getString(weekStringId)}"
        } else {
            binding.homeTvData.text =
                "${context.getString(weekStringId)}, ${context.getString(monthStringId)} $currentDay"
        }
        binding.homeTvAmPm.text = currentTime!!.substring(6).uppercase(Locale.getDefault())
    }
}